package com.xquant.common.engine.xPP.controller;

import com.xquant.common.engine.xPP.cxf.NotifyDataFacade;
import com.xquant.common.engine.xPP.cxf.XQuantPPService;
import com.xquant.common.engine.xPP.cxf.XQuantPPStub;
import com.xquant.common.engine.xPP.cxf.webservice.SerializeJsonFromFpML;
import com.xquant.common.engine.xPP.cxf.webservice.SerializeJsonFromFpMLResponse;
import com.xquant.common.engine.xPP.entity.NotifyParamL;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author B946
 * @date 25/09/2020 17:08
 * @description
 */
@RestController
@RequestMapping("pp")
public class PpController {

    @Autowired
    private XQuantPPService ppService;
    @Autowired
    private NotifyDataFacade notifyDataFacade;

    @RequestMapping(value="/fpml2Json.action")
    public Object fpml2Json(@RequestBody Map<String,Object> param){
        SerializeJsonFromFpML jsonFromFpML = new SerializeJsonFromFpML();
        jsonFromFpML.setFpml(MapUtils.getString(param,"fpml"));
        SerializeJsonFromFpMLResponse response = null;
        XQuantPPStub stub = null;
        try {
            stub = ppService.borrowStub();
            response = ppService.borrowStub().serializeJsonFromFpML(jsonFromFpML);
            if(response == null){
                return null;
            }
            int errorCode = response.getSerializeJsonFromFpMLResult().getErrorCode();
            if(errorCode < 0){
                return response.getSerializeJsonFromFpMLResult().getErrorMsg();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(stub != null){
                ppService.returnStub(stub);
            }
        }
        return response.getResult();
    }

    @RequestMapping(value="/notifyVolSurfaceTS.action")
    public Object notifyVolSurfaceTS(@RequestBody NotifyParamL paramL){
        if (paramL == null){
            paramL = new NotifyParamL();
        }

        notifyDataFacade.notifyVolSurfaceTS();
        return null;
    }
    @RequestMapping(value="/notifyDailySet.action")
    public Object notifyDailySet(@RequestBody NotifyParamL paramL){
        if (paramL == null){
            paramL = new NotifyParamL();
        }

        notifyDataFacade.notifyDailySet(paramL);
        return null;
    }

}
